package com.hyjoy.web.servlet;

import com.hyjoy.constant.Constant;
import com.hyjoy.entity.User;
import com.hyjoy.service.UserService;
import com.hyjoy.service.impl.UserServiceImpl;
import com.hyjoy.util.UUIDUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * Created by hyjoy on 2018/4/5.
 */
public class UserServlet extends BaseServlet {

    /**
     * 退出
     *
     * @param request  :
     * @param response :
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath() + "/");
        return null;
    }

    /**
     * 用户登录
     *
     * @param request  :
     * @param response :
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取用户名、密码
            String userName = request.getParameter("username");
            String password = request.getParameter("password");

            // 调用service完成登录
            UserService service = new UserServiceImpl();
            User user = service.login(userName, password);
            // 判断User
            if (user == null) {
                // 用户名密码不匹配
                request.setAttribute("msg", "用户名和密码不匹配");
                return "/jsp/login.jsp";
            }

            // 如用户不为空,继续判断用户激活状态
            if (Constant.USER_IS_ACTIVE != user.getState()) {
                // 未激活
                request.setAttribute("msg", "请先去邮箱激活，再登录!");
                return "/jsp/msg.jsp";
            }
            // 登录成功，保存用户状态
            request.getSession().setAttribute("user", user);
            // 判断是否勾选了记住用户名
            if (Constant.SAVE_NAME.equals(request.getParameter("savename"))) {
                Cookie cookie = new Cookie("savename", URLEncoder.encode(userName, "utf-8"));
                cookie.setMaxAge(Integer.MAX_VALUE);
                cookie.setPath(request.getContextPath() + "/");
                response.addCookie(cookie);
            }

            response.sendRedirect(request.getContextPath() + "/");
        } catch (Exception e) {
            request.setAttribute("msg", "用户登录失败");
            return "/jsp/msg.jsp";
        }
        return null;
    }

    /**
     * 跳转到登录页面
     *
     * @param request  :
     * @param response :
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String loginUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/jsp/login.jsp";
    }

    /**
     * 用户激活
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.接受code
            String code = request.getParameter("code");

            //2.调用service完成激活 返回值:user
            UserService us = new UserServiceImpl();
            User user = us.active(code);

            //3.判断user 生成不同的提示信息
            if (user == null) {
                //没有找到这个用户,激活失败
                request.setAttribute("msg", "激活失败,请重新激活或者重新注册~");
                return "/jsp/msg.jsp";
            }

            //激活成功
            request.setAttribute("msg", "恭喜你,激活成功了,可以登录了~");
        } catch (Exception e) {
            request.setAttribute("msg", "激活失败,请重新激活或者重新注册~");
            return "/jsp/msg.jsp";
        }
        return "/jsp/msg.jsp";
    }

    /**
     * 用户注册
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            //1.封装对象
            User user = new User();
            BeanUtils.populate(user, request.getParameterMap());

            //1.1手动封装uid
            user.setUid(UUIDUtils.getUUID());

            //1.2手动封装激活状态 state
            user.setState(Constant.USER_IS_NOT_ACTIVE);

            //1.3手动封装激活码 code
            user.setCode(UUIDUtils.getUUID());

            //2.调用service完成注册
            UserService us = new UserServiceImpl();
            us.regist(user);

            //3.页面转发 提示信息
            request.setAttribute("msg", "恭喜你,注册成功,请登录邮箱完成激活!");
        } catch (Exception e) {
            e.printStackTrace();
            //转发到 msg.jsp
            request.setAttribute("msg", "用户注册失败!");
            return "/jsp/msg.jsp";
        }

        return "/jsp/msg.jsp";
    }

    /**
     * 跳转到注册页面
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String registUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/jsp/register.jsp";
    }
}
